/**
 * Title:        Utility Class Library<p>
 * Description:  A text field class that forces integer only input.<p>
 */
package com.timber.util;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;


public class IntegerField extends JTextField
{
  public IntegerField()
  {
    super();
  }

  public IntegerField( String text )
  {
    super( text );
  }

  public IntegerField( int columns )
  {
    super( columns );
  }

  public IntegerField( String text, int columns )
  {
    super( text, columns );
  }

  protected Document createDefaultModel()
  {
    return new IntegerDocument();
  }

  static class IntegerDocument extends PlainDocument
  {
    public void insertString( int offs, String str, AttributeSet a )
      throws BadLocationException
    {
      if( str == null )
      {
	return;
      }

      try
      {
	Integer.parseInt( str );
      }
      catch( Exception ex )
      {
// 	Toolkit.getDefaultToolkit().beep();
	return;
      }

      super.insertString( offs, str, a );
    }
  }
}
